package listbyorder.access001_100.test92;

import listbyorder.utils.ListNode;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/6 8:20
 */
public class Solution1 {

    // 双指针反转链表
    public ListNode reverseBetween(ListNode head, int m, int n) {
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode h = dummy;
        ListNode t = dummy;
        int times = n - m + 1;
        for (int i = 0; i < m - 1; i++) {
            h = h.next;
        }
        for (int i = 0; i < n; i++) {
            t = t.next;
        }
        ListNode nHead = h.next;
        ListNode tail = t.next;
        t.next = null;
        ListNode next = null;
        for (int i = 0; i < times; i++) {
            next = nHead.next;
            nHead.next = tail;
            tail = nHead;
            nHead = next;
        }
        h.next = tail;
        return dummy.next;
    }
}
